package com.zhiyou100.crm.servlet.department;

import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.zhiyou100.crm.pojo.AdminBaseServlet;
import com.zhiyou100.crm.pojo.Department;
import com.zhiyou100.crm.service.DepartmentService;
import com.zhiyou100.crm.service.impl.DepartmentServiceImpl;

@WebServlet(name = "DepartmentAddServlet", urlPatterns = {"/department/add"})
public class AddServlet extends AdminBaseServlet {
    private static final long serialVersionUID = 1L;
    DepartmentService departmentService=new DepartmentServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.getRequestDispatcher("/WEB-INF/view/department/add.jsp").forward(request, response);
    doPost(request,response);
    }



    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");
        String departmentName =request.getParameter("departmentName");
        String departmentDesc = request.getParameter("departmentDesc");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long l = System.currentTimeMillis();
        String format = df.format(new Date(l));
        Timestamp time=Timestamp.valueOf(format);
        if ( departmentName!=null&&departmentDesc!=null) {
            Department department=new Department(departmentName,departmentDesc,time);
            int a=departmentService.addDepartment(department);
            response.sendRedirect(request.getContextPath() + "/department/list");
        }else {
            request.getRequestDispatcher("/WEB-INF/view/department/add.jsp").forward(request, response);
        }
    }

}
